package com.xca.znb.adapter;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.xca.znb.R;
import com.xca.znb.entity.Goods;

import java.util.List;

public class GoodsHomeAdapter extends BaseAdapter {
    private Context context ;
    private List<Goods> goodsList ;

    public GoodsHomeAdapter(Context context , List<Goods> goodsList){
        this.context = context ;
        this.goodsList = goodsList ;
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public Object getItem(int position) {
        return goodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder ;
        if (convertView == null){
            LayoutInflater inflater = LayoutInflater.from(context) ;
            convertView = inflater.inflate(R.layout.home_item , parent , false) ;
            viewHolder = new ViewHolder() ;
            viewHolder.imageView = convertView.findViewById(R.id.home_item_img);
            viewHolder.goods_name = convertView.findViewById(R.id.home_item_name);
            viewHolder.goods_id = convertView.findViewById(R.id.home_item_id);
            viewHolder.goods_price = convertView.findViewById(R.id.home_item_price);
            viewHolder.goods_unit = convertView.findViewById(R.id.home_item_unit);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag() ;
        }
        Goods goods = goodsList.get(position) ;

        String path = goods.getFirst_image().trim();
        Uri uri = Uri.parse(path);

        Glide.with(context)
                .load(path)
                .error(R.drawable.focus_del)
                .into(viewHolder.imageView);

        viewHolder.goods_name.setText(goods.getName());
        int id = goods.getId() ;
        String goodsId = String.valueOf(id);
        viewHolder.goods_id.setText(goodsId);
        viewHolder.goods_price.setText(String.valueOf(goods.getPrice()));
        viewHolder.goods_unit.setText(goods.getUnit());

        viewHolder.goods_id.setTag(position);

        return convertView;
    }

    static class ViewHolder{
        ImageView imageView;
        TextView goods_name;
        TextView goods_id;
        TextView goods_price;
        TextView goods_unit;

    }

    public Bitmap stringPathToBitmap(String imagePath) {
        try {
            // 使用BitmapFactory.decodeFile方法将图片路径转换为Bitmap对象
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath);

            if (bitmap != null) {
                return bitmap;
            } else {
                // 图片加载失败处理
               // Log.e("ImageLoadError", "Failed to load image from path: " + imagePath);
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 异常处理
            return null;
        }
    }
}
